﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.Model.Group;
using YouHP.BLL;

namespace YouHP.Pages.Group
{
    public class MyGroupListAjax:BasePage
    {
        protected List<GroupInfo> GroupList;
        protected int pageSize=6;
        protected int pageIndex=1;
        protected int groupCount=0;
        protected int pageCount = 0;
        protected string PagingString;

        protected override void ShowPage()
        {
            base.ShowPage();

            pageIndex = RequestTools.GetQueryInt("page", 1);

            GroupList = GroupBLL.Instance.GetAllGroupsByUserId(pageSize, pageIndex, UserData.Id, out groupCount);
            if (groupCount == 0)
            {
                StringBuilder sb1 = new StringBuilder();
                sb1.Append("<font style=\"font-size:14px;\">暂无</font>");
                ResponseXML(sb1);
                return;
            }

            pageCount = Utils.GetPageCount(groupCount, pageSize);
            PagingString = this.GetPagingString2(pageIndex, pageCount, "", "javascript:getMyGroupList");

            StringBuilder sb = new StringBuilder();
            int j=0;
            foreach (GroupInfo item in GroupList)
            {
                if (j > 0 && j % 3 == 0)
                {
                    sb.Append("<div class=\"line\"></div>");
                }
                sb.Append("<div class=\"groupbox\">");
                string TempUrl = "/Group/Group.aspx?gid=" + item.Id;
                sb.Append("<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\"><img class=\"pic\" src=\"" + item.Logo + "\" /></a>");
                sb.Append("<dl><dd><strong><a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">");
                if (item.GroupName.Length <= 8)
                {
                    sb.Append(item.GroupName);
                }
                else
                {
                    sb.Append(item.GroupName.Substring(0, 8) + "...");
                }
                sb.Append("</a></strong></dd>");
                sb.Append("<dt>共" + item.UserNum + "人</dt>");
                sb.Append("<dt>角色：");

                if (item.Role == 0)
                {
                    sb.Append("普通成员");
                }
                if (item.Role == 1)
                {
                    sb.Append("管理员");
                }
                if(item.Role == 2)
                {
                    sb.Append("圈主");
                }
                sb.Append("</dt></dl></div>");
                j++;
            }

            sb.Append("<div style=\"clear:both;\"></div>");
            sb.Append("<div style=\"padding:10px;\"><div class=\"pages\">"+PagingString+"</div></div>");

            ResponseXML(sb);
        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}
